<?php
/**
 * Created by PhpStorm.
 * User: Shireli
 * Date: 15-5-13
 * Time: 上午10:59
 */

//require_once 'functions.php';

/*
 * 麦西莱普统计模块类型
 * */
class Statistics {

    //时间类别
    var $today;
    var $weeek;
    var $month;
    var $year;
    var $select_time;

    //链接数据库
    public function Execute($sql_query){
    $conn = mysql_connect("localhost","root","123");
    mysql_select_db("mexrap");
    mysql_query("set names 'utf8'");
    $result = mysql_query($sql_query);
    return $result;
}

//  SELECT COUNT(*) FROM `QM_REGISTER_USER_STATISTICS` WHERE `RU_TYPE`='Web'     获取Web总数目
//  SELECT COUNT(*) FROM `QM_REGISTER_USER_STATISTICS` WHERE `RU_TYPE`='App'     获取App总数目
//  SELECT COUNT(*) FROM `QM_REGISTER_USER_STATISTICS` WHERE `RU_TYPE`='App' AND `RU_TIME` LIKE '%2015-05-13%'

    //注册用户统计
    public function registerUserStatistics($time='05/14/2015'){
        $date = date("Y-m-d");   //当前日期
        if(empty($time)){       //如果日期为空 则返回当前日期
            $time = $date;
            $region = array('乌鲁木齐','昌吉','石河子','伊犁','博乐','奎屯','克拉玛依','和田','克州','阿克苏','喀什','阿勒泰','巴州','塔城','哈密','吐鲁番','其他');
            $sql_query="select * from `QM_REGISTER_USER_STATISTICS` WHERE `RU_TIME` LIKE '%{$time}%'";  //从数据库按日期获取
            $sis=array();
            foreach($region as $val){
                $AppCount = "SELECT COUNT(*) FROM `QM_REGISTER_USER_STATISTICS` WHERE `RU_TYPE`='App' AND `RU_TIME` LIKE '%{$time}%' AND `RU_REGION` LIKE '%{$val}%'";
                $WebCount = "SELECT COUNT(*) FROM `QM_REGISTER_USER_STATISTICS` WHERE `RU_TYPE`='Web' AND `RU_TIME` LIKE '%{$time}%' AND `RU_REGION` LIKE '%{$val}%'";
                $this->Execute($sql_query);
                $resultApp = $this->Execute($AppCount);
                $resultWeb = $this->Execute($WebCount);
                $App = mysql_fetch_array($resultApp);
                $Web = mysql_fetch_array($resultWeb);
                echo '<pre>';
                $sis[$val]['Web']=$Web[0];
                $sis[$val]['App']=$App[0];
            }
        }
        else{      //如果日期不为空
            $sql_query="select * from `QM_REGISTER_USER_STATISTICS` WHERE `RU_TIME` LIKE '%{$time}%'";
            $region = array('乌鲁木齐','昌吉','石河子','伊犁','博乐','奎屯','克拉玛依','和田','克州','阿克苏','喀什','阿勒泰','巴州','塔城','哈密','吐鲁番','其他');
            $sis=array();
            foreach($region as $val){
                $AppCount = "SELECT COUNT(*) FROM `QM_REGISTER_USER_STATISTICS` WHERE `RU_TYPE`='App' AND `RU_TIME` LIKE '%{$time}%' AND `RU_REGION` LIKE '%{$val}%'";
                $WebCount = "SELECT COUNT(*) FROM `QM_REGISTER_USER_STATISTICS` WHERE `RU_TYPE`='Web' AND `RU_TIME` LIKE '%{$time}%' AND `RU_REGION` LIKE '%{$val}%'";
                $this->Execute($sql_query);
                $resultApp = $this->Execute($AppCount);
                $resultWeb = $this->Execute($WebCount);
                $App = mysql_fetch_array($resultApp);
                $Web = mysql_fetch_array($resultWeb);
                echo '<pre>';
                $sis[$val]['Web']=$Web[0];
                $sis[$val]['App']=$App[0];
                $sis[$val]['location']=$val;
            }
        }
        return $sis;
    }

    //下载用户统计
    public function downloadUserStatistics($time=''){

        $data = array();
        return $data;
    }

    //活跃用户统计
    public function activeUserStatistics($time=''){

        $data = array();
        return $data;
    }

    //付费用户统计
    public function payUserStatistics($time=''){

        $data = array();
        return $data;
    }

    //应用下载统计
    /*public function appDownloadStatistics($time='',$type='',$region=''){

        $data = array();
        return $data;
    }*/

}

$day=$_POST["name_value"];
echo("$day");
//类型数组
$type = array(1=>'Web',2=>'App',3=>'Wap',4=>'QCode');

//区域数组
$region = array('乌鲁木齐','昌吉','石河子','伊犁','博乐','奎屯','克拉玛依','和田','克州','阿克苏','喀什','阿勒泰','巴州','塔城','哈密','吐鲁番','其他');

$statistics=new Statistics();
$statistics->registerUserStatistics();
$return_array=$statistics->registerUserStatistics();
//print_r($return_array);
//echo(json_encode($return_array));


//地区会重复出现，用js获取时 直接获取地区名，然后获取相同的地区就可以